package com.chinairi.mall.service.impl;

import com.chinairi.common.annotation.DoPage;
import com.chinairi.mall.controller.viewobject.HomeAllClassVO;
import com.chinairi.mall.controller.viewobject.HomeGroundClassVO;
import com.chinairi.mall.controller.viewobject.HomeWellClassVO;
import com.chinairi.mall.model.HomeWellClass;
import com.chinairi.mall.dao.HomeWellClassMapper;
import com.chinairi.mall.service.IHomeWellClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-10-14
 */
@Service
public class HomeWellClassServiceImpl extends ServiceImpl<HomeWellClassMapper, HomeWellClass> implements IHomeWellClassService {

    @Autowired
    private HomeWellClassMapper homeWellClassMapper;
    /**
     * 获取井工程分类
     *
     * @return
     */
    @Override
    @DoPage
    public List<HomeWellClassVO> getWellClass() {
        List<HomeWellClassVO> homeWellClassVOList = homeWellClassMapper.selectWellClass();
        return homeWellClassVOList.stream().filter(vo -> vo.getHomeWellClassLevel() == 1)
                .peek(vo -> vo.setHomeWellClassVOList(getChildList(vo,homeWellClassVOList)))
                .collect(Collectors.toList());
    }

    /**
     * 分级获取子类
     * @param homeWellClassVO
     * @param list
     * @return
     */
    private List<HomeWellClassVO> getChildList(HomeWellClassVO homeWellClassVO, List<HomeWellClassVO> list) {
        return list.stream().filter(vo -> vo.getHomeWellClassParentDbid().equals(homeWellClassVO.getHomeWellClassDbid()))
                .peek(vo -> vo.setHomeWellClassVOList(getChildList(vo, list)))
                .collect(Collectors.toList());
    }
}
